package pw.hais.main.main_app.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.Response;

import java.io.File;

import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;
import pw.hais.http.image.CacheManager;
import pw.hais.http.image.LocalCache;
import pw.hais.main.R;
import pw.hais.main.main_app.app.BaseActivity;
import pw.hais.main.main_app.dao.NeiHan8Dao;
import pw.hais.main.main_app.model.NeiHan8ArticleModel;
import pw.hais.utils.EmptyUtil;

/**
 * 海涵，内涵图片、段子详情
 * Created by Hais1992 on 2015/12/16.
 */
public class HaiHanDetailsActivity extends BaseActivity implements View.OnClickListener {
    public static String INTENT_CONTENT_Id_KEY = "INTENT_CONTENT_Id_KEY";
    public static String INTENT_CONTENT_TYPE_KEY = "INTENT_CONTENT_TYPE_KEY";

    private TextView text_title;
    private TextView text_ct;
    private TextView text_content;
    private ImageView image_image;

    private NeiHan8ArticleModel neiHan8ArticleModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_haihan_article_details);

        String id = getIntent().getStringExtra(INTENT_CONTENT_Id_KEY);
        String type = getIntent().getStringExtra(INTENT_CONTENT_TYPE_KEY);
        if (EmptyUtil.emptyOfString(id) || EmptyUtil.emptyOfString(type)) {
            finish();
        }


        text_title = (TextView) findViewById(R.id.text_title);
        text_ct = (TextView) findViewById(R.id.text_ct);
        text_content = (TextView) findViewById(R.id.text_content);
        image_image = (ImageView) findViewById(R.id.image_image);
        image_image.setOnClickListener(this);


        loadDialogShow("数据加载中...");
        NeiHan8Dao.getModelById(type, id, new OnHttpListener<NeiHan8ArticleModel>() {
            @Override
            public void onSuccess(Response response, NeiHan8ArticleModel data) {
                loadDialogDismiss();

                text_title.setText(data.title);
                text_ct.setText("来源：网络收集");
                if (EmptyUtil.emptyOfString(data.image_url)) {
                    text_content.setText(Html.fromHtml(data.content));
                } else {
                    neiHan8ArticleModel = data;

                    String content = "注：\n\t\t点击图片可 使用相册打开";
                    if(data.image_url.indexOf(".gif")!=-1){
                        content = content+"\n\t\t此图是Gif图，暂不支持Gif图，抱歉！";
                    }
                    text_content.setText(content);
                    Http.displayImage(image_image, data.image_url);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_image:
                if (neiHan8ArticleModel == null) return;
                File file = new File(CacheManager.IMAGE_CACHE_DIR + LocalCache.getFileNameByUrl(neiHan8ArticleModel.image_url));
                if (file != null && file.isFile() == true) {
                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), "image/*");
                    startActivity(intent);
                }
                break;
        }
    }
}
